<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------

namespace plugins\qbt_admin_log;
use cmf\lib\Plugin;
use think\Config;
use think\db;

class QbtAdminLogPlugin extends Plugin
{
    public $info = [
        'name'        => 'QbtAdminLog',
        'title'       => '操作日志',
        'description' => '后台操作日志',
        'status'      => 1,
        'author'      => '千佰特',
        'version'     => '1.0.0',
        'demo_url'    => '',
        'author_url'  => ''
    ];

    public $hasAdmin = 1;//插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        $config = Config::get('database');
        Db::query("
            CREATE TABLE IF NOT EXISTS `{$config['prefix']}admin_log` (
            `id`  int NOT NULL AUTO_INCREMENT,
            `user_id`  varchar(100) NULL DEFAULT '' COMMENT '管理员ID' ,
            `user_name`  varchar(100) NULL DEFAULT '' COMMENT '管理员名称' ,
            `ip`  varchar(100) NULL DEFAULT '' COMMENT 'IP' ,
            `request`  varchar(100) NULL DEFAULT '' COMMENT 'url地址' ,
            `param`  text NULL COMMENT '参数' ,
            `content`  text NULL COMMENT '描述' ,
            `addtime`  int NULL COMMENT '添加时间' ,
            PRIMARY KEY (`id`)
            )
        ");
        return true;
    }

    // 插件卸载
    public function uninstall()
    {
        $config = Config::get('database');
        Db::query("DROP TABLE `{$config['prefix']}admin_log`");
        return true;
    }

    public function adminInit()
    {
        $time = time();
        $url = request()->url();
        $lists = Db::name('plugin')->where(['name'=>'QbtAdminLog'])->find();
        $fosn = $lists['config'];
        $conf = json_decode($fosn,true);

        $array = explode("\n", $conf['textarea']); 
        $title = '';
        $param = request()->param();
        foreach ($array as $key => $value) {
        	$arr = explode("|", $value); 
        	$pib = $arr['0'];
            if(!empty($pib) && strpos($url,$pib) !==false){
                $content = $this->rule2content($arr[1], $param, '');
                $data = [
                    'user_id'=>cmf_get_current_admin_id(),
                    'user_name'=>session('name'),
                    'ip'=>get_client_ip(),
                    'request'=>request()->url(),
                    'param'=>json_encode($param),
                    'content'=>$content,
                    'addtime'=>time()
                ];
                if($arr[1] != $content){
                    Db::name('admin_log')->insert($data);
                }
                
            }

        }
    }

    /**
     * 规则转换显示
     * @param $rule 规则
     * @param $param 参数
     * @param $param_per 参数前缀，一般用于数组
     * @return mixed 转换后的内容
     */
    private function rule2content($rule, $param, $param_per){
        if(!is_array($param)) return $rule;
        foreach($param as $k=>$v){
            if(is_array($v)){
                if($k == 'ids'){
                    foreach ($v as $key => $value) {
                        if($key==0){
                            $shuj = $value;
                        }else{
                            $shuj .=','.$value;
                        }
                    }
                    $rule = str_replace('{'.$k.'}', $shuj, $rule);
                }else{
                    $rule = $this->rule2content($rule, $v, $k);
                }
            }else{
                if($param_per!='') $param_per .= '.';
                $rule = str_replace('{'.$k.'}', $v, $rule);
            }
        }
        return $rule;
    }

}
